/***
 * @author 学相伴
 * @description 通用配置菜单和导航栏
 * @date 2021/08/13
 * 
 * 图标参考地址：https://www.layui.com/doc/element/icon.html
 */

// 左侧导航菜单配置
var slideList = [
	{
        "name": "控制台",
        "expand":false,
        "icon": "layui-icon layui-icon-website",
        "url": "/admin/news/list",
    },
    {
        "name": "新闻管理",
        "icon": "layui-icon layui-icon-home", 
        "url":"javascript:void(0);",
        "expand":true,
        "childList": [
            {
                "name": "新闻分类",
                "active":false,//激活多个，只会选择第一个
                "icon": "layui-icon layui-icon-website",
                "url": "/admin/newscategory/list",
            },
            {
                "name": "新闻管理",
                "active":true,//激活多个，只会选择第一个
                "icon": "layui-icon layui-icon-transfer", 
                "url": "/admin/news/list",
            }
        ]
    },
    {
        "name": "选车购车",
        "icon": "layui-icon layui-icon-home",
        "url":"javascript:void(0);",
        "expand":false,
        "childList": [
            {
                "name": "产品分类",
                "active":false,//激活多个，只会选择第一个
                "icon": "layui-icon layui-icon-website",
                "url": "/admin/productcategory/list",
            },
            {
                "name": "新闻管理",
                "active":false,//激活多个，只会选择第一个
                "icon": "layui-icon layui-icon-transfer",
                "url": "/admin/product/list",
            }
        ]
    },
    {
        "name": "服务网络",
        "icon": "layui-icon layui-icon-user", 
        "url":"javascript:void(0);",
        "expand":true,
        "childList": [
            {
                "name": "服务点管理",
                "active":false,//激活多个，只会选择第一个
                "icon": "layui-icon layui-icon-log", 
                "url": "/admin/distribute/list"
            }
        ]
    },
    {
        "name": "人才招聘",
        "icon": "layui-icon layui-icon-user",
        "url":"javascript:void(0);",
        "expand":true,
        "childList": [
            {
                "name": "招聘分类",
                "active":false,//激活多个，只会选择第一个
                "icon": "layui-icon layui-icon-log",
                "url": "/admin/zhaopincategory/list"
            },
            {
                "name": "招聘管理",
                "active":true,//激活多个，只会选择第一个
                "icon": "layui-icon layui-icon-star",
                "url": "/admin/zhaopin/list"
            }
        ]
    },
    {
        "name": "用户中心",
        "icon": "layui-icon layui-icon-user",
        "url":"javascript:void(0);",
        "expand":true,
        "childList": [
            {
                "name": "添加用户",
                "active":false,//激活多个，只会选择第一个
                "icon": "layui-icon layui-icon-log",
                "url": "/admin/user/add"
            },
            {
                "name": "用户列表",
                "active":false,//激活多个，只会选择第一个
                "icon": "layui-icon layui-icon-star",
                "url": "/admin/user/list"
            }
        ]
    },
    {
        "name": "集团介绍",
        "expand":false,
        "icon": "layui-icon layui-icon-video", 
        "url" : "/admin/intro/list",
         "childList": []
    }

];

// 头部下拉菜单配置
var menuList = [
	{
        "name": "首页",
        "subtitle":"",
        "icon": "iconfont iconhome",
        "url": "/admin"
    }
];


// 菜单初始化对象
var KsdMenu = {
	// 初始化
	initMenu:function(){
		var html = "";
		for(var i = 0; i < menuList.length; i++) {
			var menu = menuList[i];
			html+="<li class='items item_"+i+"'   data-href='"+menu.url+"'>"+
            "    <a href='javascript:void(0);' style='width:160px;text-align:left' class='fl ksd-settings2 flitems'><i class='"+menu.icon+"'></i>"+menu.name+"</a>"+
            "    <a href='javascript:void(0);' class='fr ksd-settings2 fritems'>"+menu.subtitle+"</a>"+
            "</li>";
		}
		
		
		$(".ksd-index-menulist").html(html);
		
		// 切换iframe
		$(".ksd-index-menulist").find(".items").on("click", function() {
			var href = $(this).data("href");
			$(".layadmin-iframe").attr("src", href);
			$(".layui-nav-child").find("dd").removeClass("layui-this");
			$(".ksd-links[lay-href='"+href+"']").parent().addClass("layui-this");
		});
		// 事件初始化
		KsdMenu.toggleMenu();
		// 退出登录
		KsdMenu.logout();
	},
	
	// 初始化左侧导航菜单
	initSlide: function() {
		var html = "";
		for(var i = 0; i < slideList.length; i++) {
			var menu = slideList[i];
			var chtml = menu.childList && menu.childList.length > 0 ? '<i class="layui-icon layui-icon-down layui-nav-more"></i>' :''; 
			html += '<li  class="layui-nav-item '+(menu.expand ?"layui-nav-itemed":"")+'">' +
				'	<a href="javascript:;"  lay-pindex="'+i+'" lay-href="' + menu.url + '" class="select-item-'+i+'  main-links" lay-tips="' + menu.name + '" lay-direction="2">' +
				'		<i class="' + menu.icon + '"></i>' +
				'		<cite>' + menu.name + '</cite>' +chtml+'</a>';
			if(menu.childList && menu.childList.length > 0 ){	
				html +='	<dl class="layui-nav-child">'
				for(var j = 0; j < menu.childList.length; j++) {
					var cmenu = menu.childList[j];
					//var checked = i == 0 && j == 0 ? "layui-this" : "";
					var checked = cmenu.active ? "layui-this" : "";
					html += '<dd class="' + checked + '" lay-href="' + cmenu.url + '">' +
						'		<a class="select-item-'+i+'-'+j+'  ksd-links" lay-pindex="'+i+'" lay-cindex="'+i+'-'+j+'" lay-href="' + cmenu.url + '"><i class="' + cmenu.icon + '"></i>' + cmenu.name + '</a>' +
						'</dd>';
				}
				html += '</dl>';
			}
			html += '</li>';
		}

		$("#LAY-system-side-menu").html(html);
		this.initEvents(function(){
			var cindex = localStorage.getItem("ksd-href-cindex");
			var pindex = localStorage.getItem("ksd-href-pindex");
			if(!cindex && !pindex){		
				
				var $current = $("#LAY-system-side-menu").find(".layui-this").eq(0);
				var href = $current.attr("lay-href");
				$(".layui-this").removeClass("layui-this");
				$current.addClass("layui-this");
				$(".layadmin-iframe").attr("src", href);
			}else{
				
				if(cindex && pindex){
					$(".select-item-"+pindex).eq(0).trigger("click");
					$(".select-item-"+cindex).eq(0).trigger("click");
				}else{
					$(".select-item-"+pindex).eq(0).trigger("click");
				}
			}
		});
	},

	// 事件初始化
	initEvents: function(callback) {
		
		// 单节点切换iframe
		$("#LAY-system-side-menu").find(".main-links").on("click", function() {
			if($(this).parent().hasClass("layui-this"))return;
			var href = $(this).attr("lay-href");
			if(!href)return;
			$(".layadmin-iframe").attr("src", href);
			var pindex  = $(this).attr("lay-pindex");
			localStorage.setItem("ksd-href-pindex",pindex);
			localStorage.setItem("ksd-href-cindex","");
		});

		
		// 切换iframe
		$("#LAY-system-side-menu").find(".ksd-links").on("click", function() {
			$("#LAY-system-side-menu").find("dd").removeClass("layui-this");
			$(this).parent().addClass("layui-this");
			var href = $(this).attr("lay-href");
			if(!href)return;
			$(".layadmin-iframe").attr("src", href);
			localStorage.setItem("ksd-href-pindex",$(this).attr("lay-pindex"));
			localStorage.setItem("ksd-href-cindex",$(this).attr("lay-cindex"));
		});
		
		// 折叠展开
		$("#LAY-system-side-menu").find(".layui-nav-item").find("a").on("click",function(e){
			$(this).parent().toggleClass("layui-nav-itemed").siblings().removeClass("layui-nav-itemed");
			e.stopPropagation();
		});
		
		
		callback && callback();
	},
	
	toggleMenu:function(){
        $("#navbarDropdown").on("click",function(e){
            $(".ksd-logindrop").toggle();
        })

        $(".ksd-logindrop").on("mouseleave",function(){
            $(this).hide();
        })
	},
	
	// 退出登录
	logout:function(){
		$(".ksd-logout").on("click",function(){
			//询问框
			layer.confirm('你确定要退出吗？', {
			  btnAlign:"c",
			  textAlign:"c",
			  btn: ['确定','取消'] //按钮
			}, function(){
			  $.post("/admin/logout",function(){
                  window.location.href = "/admin/login";
              })
			}, function(){
			  layer.msg('取消离开了', {
			    time: 2000, //20s后自动关闭
			  });
			});
		});
	}
};

// 页面初始化
layui.use(['jquery'], function() {
	// 1: 初始化jquery
	window.jQuery = window.$ = layui.jquery;
	// 2: 初始菜单
	KsdMenu.initSlide();
	// 3:初始化下拉菜单
	KsdMenu.initMenu();
})